<%-- 
    Document   : listtransaction
    Created on : Dec 6, 2012, 11:14:53 AM
    Author     : Quang Nhat
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>General Journal</title>
        <script src="js/xml.js" type="text/javascript"></script>
        <script src="js/table.js" type="text/javascript"></script>
        <script type="text/javascript">
            var xmlContent='${requestScope.xmlContent}';
            var doc = getXMLDOM();
            doc.loadXML(xmlContent);
            var pageSize=${requestScope.pageSize};
            var numPage=${requestScope.numPage};
            var page=1;
            var saveParam = "txtMinDate=" + "${param.txtMinDate}"+"&txtMaxDate=" + "${param.txtMaxDate}"+"&txtText=" + "${param.txtText}";
            var accList = getAccountList();
            function getAccountList() {
                dom = getXMLDOM();
                dom.load("Accountant/XML?action=listAccount");
                var acc = [];
                
                var root = dom.documentElement;
                for(var i = 0; i < root.childNodes.length; i++) {
                    var node = root.childNodes[i];
                    var accNum = node.getAttribute('AccountNumber');
                    var accName = node.firstChild.firstChild.nodeValue;
                    acc[accNum] = accName;
                }
                return acc;
            }
            function addTranToTable(no, node) {
                var id = node.getAttribute("Id");
                var cells = [];
                var parts = node.getAttribute('Date').split("-");
                var date = parts[2] + "/" + parts[1] + "/" + parts[0];
                cells[1] = date;
                cells[2] = "";
                cells[3] = "";
                cells[0] = no;
                var details = [];
                for(var i = 0; i < node.childNodes.length; i++) {
                    var child = node.childNodes[i];
                    if(child.nodeName == "Explanation") {
                        cells[2] = child.firstChild.nodeValue;
                    } else {
                        details.push(child);
                    }
                }
                
                var mRow = document.createElement("tr");
                mRow.id = "mRow" + id;
                mRow.onclick = function(){
                    drow = document.getElementById("dRow"+id)
                    if(drow.style.display == "none"){
                        drow.style.display =""
                    }else{
                        drow.style.display = "none"
                    }
                }
                var cell;
                for (var i = 0; i < cells.length; i++) {
                    cell = document.createElement("td");
                    debugger;
                    cell.innerHTML = cells[i];
                    mRow.appendChild(cell);
                }
                var bEdit = document.createElement("a");
                bEdit.href = "Accountant?action=editTransaction&Id=" + id +"&" + saveParam; 
                bEdit.innerHTML = "Edit";
                cell.appendChild(bEdit);
                document.getElementById("tranList").appendChild(mRow);

                var table = document.createElement("table");
                
                table.id = "d" + id;
                var dRow = document.createElement("tr");
                dRow.id = "dRow" + id;
                dRow.style.display = "none";
                var dRowCell = document.createElement("td");
                dRowCell.setAttribute("colspan", cells.length);
                dRowCell.appendChild(table);
                dRow.appendChild(dRowCell);
                document.getElementById("tranList").appendChild(dRow);
                
                var tHead = document.createElement("thead");
                var hRow = document.createElement("tr");
                var c = document.createElement("th");
                c.innerHTML = "Account";
                hRow.appendChild(c);
                c = document.createElement("th");
                c.setAttribute("style", "text-align: left");
                c.innerHTML = "Debit";
                hRow.appendChild(c);
                c = document.createElement("th");
                c.setAttribute("style", "text-align: left");
                c.innerHTML = "Credit";
                hRow.appendChild(c);
                tHead.appendChild(hRow);
                table.appendChild(tHead);
                table.style.width = "80%";
                
                var body = document.createElement("tbody");
                table.appendChild(body);
                for (var i=0; i< details.length; i++) {
                    var row = document.createElement("tr");
                    var cell = document.createElement("td");
                    var accountId = Number(details[i].getAttribute("AccountId"));
                    var isDebit = details[i].getAttribute("IsDebit");
                    var amount = details[i].getAttribute("Amount");
                    
                    cell.innerHTML = accountId + " - " + accList[accountId];
                    row.appendChild(cell);
                    
                    if (isDebit == "true") {
                        cell = document.createElement("td");
                        cell.innerHTML = amount;
                        row.appendChild(cell);
                        cell = document.createElement("td");
                        row.appendChild(cell);
                    } else {
                        cell = document.createElement("td");
                        row.appendChild(cell);
                        cell = document.createElement("td");
                        cell.innerHTML = amount;
                        row.appendChild(cell);
                    }
                    body.appendChild(row);
                }
            }
            function loadPage() {
                var root = doc.documentElement;
                var from = (page - 1) * pageSize;
                var numItem = root.childNodes.length;
                var to = Math.min(page * pageSize, numItem);
                clearTable("tranList");
                for(var i = from; i < to; i++) {
                    debugger;
                    var node = root.childNodes[i];
                    addTranToTable(i + 1, node);
                }
                document.getElementById("p" + page).removeAttribute("href");
            }
            function changePage(p) {
                document.getElementById("p" + page).setAttribute("href", "javascript:changePage("+page+")");
                page = p;
                loadPage();
            }
            function init() {
                var paging = document.getElementById("paging");
                var count = 0;
                var btnAdd = document.getElementById("btnAdd");
                btnAdd.href+= "&" + saveParam;
                var btnExport = document.getElementById("btnExport");
                btnExport.href+= "&" + saveParam;
                for (var i = 0; i < paging.childNodes.length; i++) {
                    if (paging.childNodes[i].nodeName.toLowerCase() == "a"){
                        count++;
                        paging.childNodes[i].id = "p" + count;
                    }
                }
                loadPage();
            }
            function validDate(txt) {
                var parts = txt.split("/");
                if (parts.length != 3) {
                    return false;
                }
                var date = new Date(parts[2], parts[1] - 1, parts[0]);
                return (date != "Invalid Date");
            }
            function valid() {
                var txtMinDate = document.getElementById("txtMinDate").value;
                var txtMaxDate = document.getElementById("txtMaxDate").value;
                if (!validDate(txtMinDate)) {
                    alert("Invalid From Date.");
                    return false;
                }
                if (!validDate(txtMaxDate)) {
                    alert("Invalid To Date.");
                    return false;
                }
                return true;
            }
        </script>
    </head>
    <body onload="init();">
        <%@include file="/WEB-INF/jspf/Header.jspf" %>
        <h1>General Journal</h1>
        <form action="Accountant" class="form" style="overflow: hidden;width:300px" onsubmit="return valid();">
            <div><label>From Date: <input id="txtMinDate" type="text" name="txtMinDate" value="${param.txtMinDate}" /></label></div>
            <div><label>To Date: <input id="txtMaxDate" type="text" name="txtMaxDate" value="${param.txtMaxDate}" /></label></div>
            <div><label>Text: <input type="text" name="txtText" value="${param.txtText}" /></label></div>
            <div>
                <input type="hidden" name="action" value="listTransaction" />
                <input type="submit" value="Search" />
            </div>
        </form>
        <a id="btnAdd" href="Accountant?action=addTransaction" class="button" style="text-decoration: none; float: left; clear: both;height: 18px;margin-bottom: 10px; font-size: 14px">Add new transaction</a>
        <a id="btnExport" href="Accountant?action=pdfListTransaction" class="button" style="text-decoration: none; float: left; height: 18px;margin-bottom: 10px; margin-left: 20px; font-size: 14px">Export to PDF</a>

        <table border="1" id="tranList" style="width: 500px">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Date</th>
                    <th>Explanation</th>
                    <th>Edit</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                </tr>
            </tbody>
        </table>
        <div style="margin-top: 15px" id="paging">
            <c:forEach begin="1" end="${requestScope.numPage}" var="p">
                <a href="javascript:changePage(${p})">${p}</a>
            </c:forEach>
        </div>
    </body>
</html>
